
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title> 文献医案库 </title>
  <link rel="stylesheet" type="text/css" href="/css/style.css" />

  <style type="text/css">
    *, body {
      margin: 0px;
      font-family: '微软雅黑',serif;
    }
    .button-container {
      float: right;
    }
  </style>
  <script src="/js/jquery-1.10.2.min.js"></script>
  <script src="/js/generic.js"></script>
  <script src="/js/fypage.js"></script>
</head>

<body>
<table class="maintable"style="width:100%">
  <tr>
    <td class="rightmaintd" valign="top">
      <div style="height:40px;text-align:center;background-color:#009966">
        <p style="font-weight:bold;padding-top:10px;font-size:larger;color:white">文献医案</p>
      </div>
      <div class="searchdiv">
        <input type="button" id="btnAdd"  value="+添加医案" class="xinzengbtn" onclick="create()">
        <input type="button" value="+批量添加" class="xinzengbtn" onclick="window.location.href='/wxya/batchAdd'">
        <input type="button" value="导出当前表格数据" class="chaxunbtn" onclick="table2Excel('文献医案信息')" style="float:right; width: 150px" />
      </div>
      <div class="searchdiv">
        <label>医案编号：<input type="text"  id="searchBh" onkeypress="enterKeyPress(event)"></label>
        <label>医生姓名：<input type="text" style="width:5%" id="searchYs" onkeypress="enterKeyPress(event)"></label>
        <label>书名：<input type="text" style="width:8%" id="searchSm" onkeypress="enterKeyPress(event)"></label>
        <label>文献作者：<input type="text"  style="width:6%" id="searchZz" onkeypress="enterKeyPress(event)"></label>
        <label>患者姓名：<input type="text"  style="width:6%" id="searchHz" onkeypress="enterKeyPress(event)"></label>
        <label>处方用药：<input type="text"  style="width:5%" id="search1" onkeypress="enterKeyPress(event)"></label>
        <label>西医诊断：<input type="text"  style="width:5%" id="search2" onkeypress="enterKeyPress(event)"></label>
        <label>中医诊断：<input type="text"  style="width:5%" id="search3" onkeypress="enterKeyPress(event)"></label>
        <label>病症1：<input type="text"  style="width:8%" id="searchbz1" onkeypress="enterKeyPress(event)"></label>
        <label>病症2：<input type="text"  style="width:8%" id="searchbz2" onkeypress="enterKeyPress(event)"></label>
        <label>病症3：<input type="text"  style="width:8%" id="searchbz3" onkeypress="enterKeyPress(event)"></label>
        <label>起始年：<input type="text"  style="width:5%" id="searchyear1" onkeypress="enterKeyPress(event)"></label>
        <label>月：<input type="text"  style="width:5%" id="searchmonth1" onkeypress="enterKeyPress(event)"></label>
        <label>日：<input type="text"  style="width:5%" id="searchday1" onkeypress="enterKeyPress(event)"></label>
        <label>结束年：<input type="text"  style="width:5%" id="searchyear2" onkeypress="enterKeyPress(event)"></label>
        <label>月：<input type="text"  style="width:5%" id="searchmonth2" onkeypress="enterKeyPress(event)"></label>
        <label>日：<input type="text"  style="width:5%" id="searchday2" onkeypress="enterKeyPress(event)"></label>
        <select id="shenhe" onchange="startselect()">
          <option value="" selected>所有</option>
          <option value="保存">保存</option>
          <option value="一审">一审</option>
          <option value="通过">通过</option>
        </select>

        <button class="searchbtn" onclick="startselect()" >
          <img src="/images/fdj.png" alt="" class="searchfdj">
        </button>

      </div>


      <table class="neirontbiao" id="tbl_list" style="width:100%">
        <tr>

        </tr>
        <table class="neirontbiao" id="add" style="width: 100%"></table>

      </table>
      <div class="button-container">
        <button id="prevButton">上一页</button>
        <button id="nextButton">下一页</button>
      </div>
      每页显示条数：
      <select id="pageSizeSelect" onchange="changePageSize();">
        <option value="10">10</option>
        <option value="25">25</option>
        <option value="50">50</option>
        <option value="100" selected>100</option>
        <option value="200">200</option>
      </select>
      <div class="pagediv" id="pager">
        <label>第
          <input type="text" id="page" width="5px" onkeypress="pageSearch(event)">
          页
        </label>
        <input type="button" value="跳转" onclick="pageOn()">
        <p id="sumPageId">place</p>

      </div>

    </td>
  </tr>
</table>
<p class="shownone" id="Userrole" th:text="${roleType}"></p>
<!--userid用户类型-->
</body>
</html>
<script>
  var role;
  var roleCheck = true;
  usercheckFun();
  function usercheckFun(){
    role = document.getElementById('Userrole').textContent;
    if(role=='二级审核'||role=='教师审核'||role=='名医专家'||role=='学生'||role=='输入者'){
      roleCheck = false;
    }
  }
  function start(){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/wxya/', true);
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4 && xhr.status === 200) {
        dataSource = JSON.parse(xhr.responseText);
        dataSource.forEach(function(obj) {
          for (var key in obj) {
            if (obj.hasOwnProperty(key) && obj[key] === null) {
              obj[key] = ''; // 将属性值为 null 的属性替换为空字符串
            }
          }
        });
        data = JSON.parse(JSON.stringify(dataSource));
        renderData();
      }
    };

    xhr.send();
  }

  var bh = document.getElementById('searchBh');
  var ys = document.getElementById('searchYs');
  var sm = document.getElementById('searchSm');
  var zz = document.getElementById('searchZz');
  var hz = document.getElementById('searchHz');
  var cf = document.getElementById('search1');
  var xy = document.getElementById('search2');
  var zy = document.getElementById('search3');
  var bz1 = document.getElementById('searchbz1');
  var bz2 = document.getElementById('searchbz2');
  var bz3 = document.getElementById('searchbz3');
  var year2 = document.getElementById('searchyear2');
  var year1 = document.getElementById('searchyear1');
  var month1 = document.getElementById('searchmonth1');
  var month2 = document.getElementById('searchmonth2');
  var day1 = document.getElementById('searchday1');
  var day2= document.getElementById('searchday2');
  var shenhe = document.getElementById('shenhe');
  var pageChage=document.getElementById('page');
  var sumPageShow=document.getElementById('sumPageId');
  var pageSize = 50; // 每页显示的数据量
  var currentPage = 1; // 当前页码
  var dataRender=[];  //每页显示的数据，是数据
  var pageSum=0;  //总共的页数
  var dataSource=[];   //一共有的json数据，源数据
  var data=[];  // 操作的数据，防止错误，可以由源数据赋值
  var dataSource1=[];
  // 使用AJAX请求获取JSON数据
  start();
  function renderData(){    //渲染页面函数
    pageSum=Math.ceil(data.length/(pageSize));
    dataRender=data.slice((currentPage-1)*pageSize,pageSize*currentPage);
    var container = document.getElementById('tbl_list');
    var html = '<tr class="neirontbiaotou"><th class="shownone">医案编号</th><th width="7%">患者姓名</th><th width="5%">性别</th><th width="5%">年龄</th><th width="12%" class="noExl">就诊时间</th><th width="7%">医生</th><th width="13%">书名</th><th width="5%">起始页</th><th width="5%">终止页</th><th width="12%">初始录入人</th><th width="12%">初始录入时间</th><th with="10%">状态</th><th class="noExl">操作</th>' +
            '<th class="shownone">编者/主编</th><th class="shownone">就诊日期：年</th><th class="shownone">月</th><th class="shownone">日</th><th class="shownone">舌脉总体情况</th><th class="shownone">现病史</th></tr>';
    if (dataRender.length == 0) {
      html += '<tr><td colspan="7" style="text-align:center;height:50px;">暂无医案信息</td></tr>';
    }

    for (var i = 0; i < dataRender.length; i++) {
      var item = dataRender[i];
      html += '<tr>';
      html += '<td class="shownone">' + item.biaohao + '</td>';
      html += '<td>' + item.xingming + '</td>';
      html += '<td>' + item.xingbie + '</td>';
      html += '<td>' + item.nianling + '</td>';
      // html += '<td class="noExl">' + formDate(item.jiuzhenshijian) + '</td>';
      html += '<td class="noExl">' + formDate1(item.year,item.month,item.day) + '</td>';
      html += '<td>' + item.jiuzhenyisheng + '</td>';
      html += '<td>' + item.shuming + '</td>';
      html += '<td>' + item.yema + '</td>';
      html += '<td>' + item.yemaEnd + '</td>';
      html += '<td>' + item.createuser + '</td>';
      html += '<td>' + formDate(item.createtime) + '</td>';
      html += '<td>' + item.shenhe + '</td>';
      html += '<td class="shownone">' + item.zuozhe + '</td>';
      html += '<td class="shownone">' + item.year + '</td>';
      html += '<td class="shownone">' + item.month + '</td>';
      html += '<td class="shownone">' + item.day + '</td>';
      html += '<td class="shownone">' + item.shezhen + '</td>';
      html += '<td class="shownone">' + item.xianbingshi + '</td>';
      // html += '<td class="shownone">' + item.month + '</td>';

      if(roleCheck||item.shenhe=='保存'){
        html += '<td class="noExl">' + '<a href="javascript:update(\'' + item.id + '\')">编辑</a>' ;
        html += '&nbsp;<a style=";color:#0c531f" href="javascript:deleteD(\'' + item.id + '\')">删除</a>';
      }
      else html += '<td class="noExl">' + '<a href="javascript:update(\'' + item.id + '\')">查看</a>' ;
      html += '</td></tr>'
    }
    pageChage.value=currentPage;
    sumPageShow.textContent="共 "+pageSum+" 页";
    container.innerHTML = html;
  }
  function formDate(sqlDate){
    if(sqlDate=='')
      return '';
// 将字符串转换为 JavaScript Date 对象
    const date = new Date(sqlDate);
// 获取年、月、日
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0"); // 注意月份需要加 1
    const day = String(date.getDate()).padStart(2, "0");
// 构建年-月-日格式的字符串
    return `${year}-${month}-${day}`;
  }
  function formDate1(year,month,day){
if(year==''){
  if(month==''){
    if(day=='') return '';
    else return `${day}日`;
  }
  else {
    if(day=='') return `${month}月`;
    else return `${month}月${day}日`;
  }
}
else {
  if(month==''){
    if(day=='') return `${year}年`;
    else return `${year}年${day}日`;
  }
  else {
    if(day=='') return `${year}年${month}月`;
    else return `${year}年${month}月${day}日`;
  }
}

  }
  function nextOrPreOnClick(){
    //上一页
    var prevButton = document.getElementById('prevButton');
    prevButton.addEventListener('click', function() {
      currentPage--;
      if(currentPage==0)
        currentPage=pageSum;
      renderData();

    });

    // 下一页按钮点击事件处理函数
    var nextButton = document.getElementById('nextButton');
    nextButton.addEventListener('click', function() {
      currentPage=(++currentPage)%pageSum;
      currentPage=currentPage==0?pageSum:currentPage;
      renderData();
    });
  }
  function changePageSize(){
    var pageSizeSelect = document.getElementById('pageSizeSelect');
    pageSize = parseInt(pageSizeSelect.value);
    currentPage=1;
    renderData();
  }
  nextOrPreOnClick();
  function startselect(){
    var cfVal = cf.value;
    var xyVal = xy.value;
    var zyVal = zy.value;
    if(cfVal=="") cfVal=null;
    if(xyVal=="") xyVal=null;
    if(zyVal=="") zyVal=null;
    if(cfVal==null&&xyVal==null&&zyVal==null)
      search();
    else {
      var formData =new FormData();
      formData.append('chufang',cfVal);
      formData.append('xiyi',xyVal);
      formData.append('zhongyi',zyVal);
      fetch('/wxya/inquiry', {
        method: 'POST',
        body: formData,
      })
              .then(response => {
                if (!response.ok) {
                  throw new Error('网络请求错误');
                }
                return response.text(); // 将响应数据解析为文本
              })
              .then(data1 => {
                // 在页面上显示后端返回的字符串
                dataSource1 = JSON.parse(data1);
                dataSource1.forEach(function(obj) {
                        for (var key in obj) {
                          if (obj.hasOwnProperty(key) && obj[key] === null) {
                            obj[key] = ''; // 将属性值为 null 的属性替换为空字符串
                          }
                        }
                      });
                search2();

              })
              .catch(error => {
                alert(error);
                console.error('发生错误:', error);
              });
    }
  }
  function search() {
    currentPage = 1;
    data = [];
    var bhVal = bh.value.toUpperCase();
    var ysVal = ys.value.toUpperCase();
    var smVal = sm.value.toUpperCase();
    var zzVal = zz.value.toUpperCase();
    var hzVal = hz.value.toUpperCase();
    var bz1Val = bz1.value.toUpperCase();
    var bz2Val = bz2.value.toUpperCase();
    var bz3Val = bz3.value.toUpperCase();
    var year2Val = year2.value;
    var year1Val = year1.value;
    var month1Val = month1.value;
    var month2Val = month2.value;
    var day1Val =day1.value;
    var day2Val= day2.value;
    var  DateNumber1=0;
    var  DateNumber2=0;
    DateNumber1+=year1Val*366;
    DateNumber1+=month1Val*31;
    DateNumber1+=day1Val*1;
    DateNumber2+=year2Val*366;
    DateNumber2+=month2Val*31;
    DateNumber2+=day2Val*1;
    var shenheVal = shenhe.value.toUpperCase();
    if (bhVal == ""&&ysVal==""&&smVal==""&&zzVal==""&&hzVal==""&&shenheVal==""&&bz1Val==""&&bz2Val==""&&bz3Val==""&&
      year2Val==""&&year1Val==""&&month1Val==""&&month2Val==""&&day1Val&&day2Val=="") {
      data = JSON.parse(JSON.stringify(dataSource));
    }
    else {
      for (var i = 0; i < dataSource.length; i++) {
        if(DateNumber2==0&&DateNumber1==0){
          if (dataSource[i].biaohao.toUpperCase().includes(bhVal)
                  && dataSource[i].jiuzhenyisheng.toUpperCase().includes(ysVal)
                  && dataSource[i].shuming.toUpperCase().includes(smVal)
                  && dataSource[i].zuozhe.toUpperCase().includes(zzVal)
                  && dataSource[i].xingming.toUpperCase().includes(hzVal)
                  && dataSource[i].shenhe.toUpperCase().includes(shenheVal)
                  && dataSource[i].xianbingshi.toUpperCase().includes(bz1Val)
                  && dataSource[i].xianbingshi.toUpperCase().includes(bz2Val)
                  && dataSource[i].xianbingshi.toUpperCase().includes(bz3Val)) {
            data.push(dataSource[i]);
          }
        }
        else {
          if (dataSource[i].biaohao.toUpperCase().includes(bhVal)
                && dataSource[i].jiuzhenyisheng.toUpperCase().includes(ysVal)
                && dataSource[i].shuming.toUpperCase().includes(smVal)
                && dataSource[i].zuozhe.toUpperCase().includes(zzVal)
                && dataSource[i].xingming.toUpperCase().includes(hzVal)
                && dataSource[i].shenhe.toUpperCase().includes(shenheVal)
                && dataSource[i].xianbingshi.toUpperCase().includes(bz1Val)
                && dataSource[i].xianbingshi.toUpperCase().includes(bz2Val)
                && dataSource[i].xianbingshi.toUpperCase().includes(bz3Val)
                  &&( (((dataSource[i].year*366+dataSource[i].month*31+dataSource[i].day*1)>=DateNumber1)
                  && ((dataSource[i].year*366+dataSource[i].month*31+dataSource[i].day*1)<=DateNumber2))
                  || (((dataSource[i].month*31+dataSource[i].day*1)>=DateNumber1)
                          && ((dataSource[i].month*31)<=DateNumber2)&&year2Val==""&&year1Val=="")) )
            data.push(dataSource[i]);
        }
      }
    }
    renderData();
  }
  function search2() {
    currentPage = 1;
    data = [];
    var bhVal = bh.value.toUpperCase();
    var ysVal = ys.value.toUpperCase();
    var smVal = sm.value.toUpperCase();
    var zzVal = zz.value.toUpperCase();
    var hzVal = hz.value.toUpperCase();
    var bz1Val = bz1.value.toUpperCase();
    var bz2Val = bz2.value.toUpperCase();
    var bz3Val = bz3.value.toUpperCase();
    var year2Val = year2.value;
    var year1Val = year1.value;
    var month1Val = month1.value;
    var month2Val = month2.value;
    var day1Val =day1.value;
    var day2Val= day2.value;
    var  DateNumber1=0;
    var  DateNumber2=0;
    DateNumber1+=year1Val*366;
    DateNumber1+=month1Val*31;
    DateNumber1+=day1Val*1;
    DateNumber2+=year2Val*366;
    DateNumber2+=month2Val*31;
    DateNumber2+=day2Val*1;
    var shenheVal = shenhe.value.toUpperCase();
    if (bhVal == ""&&ysVal==""&&smVal==""&&zzVal==""&&hzVal==""&&shenheVal==""&&bz1Val==""&&bz2Val==""&&bz3Val==""&&
            year2Val==""&&year1Val==""&&month1Val==""&&month2Val==""&&day1Val&&day2Val=="") {
      data = JSON.parse(JSON.stringify(dataSource1));
    }
    else {
      for (var i = 0; i < dataSource1.length; i++) {
        if(DateNumber2==0&&DateNumber1==0){
        if (dataSource1[i].biaohao.toUpperCase().includes(bhVal)
                && dataSource1[i].jiuzhenyisheng.toUpperCase().includes(ysVal)
                && dataSource1[i].shuming.toUpperCase().includes(smVal)
                && dataSource1[i].zuozhe.toUpperCase().includes(zzVal)
                && dataSource1[i].xingming.toUpperCase().includes(hzVal)
                && dataSource1[i].shenhe.toUpperCase().includes(shenheVal)
                && dataSource1[i].xianbingshi.toUpperCase().includes(bz1Val)
                && dataSource1[i].xianbingshi.toUpperCase().includes(bz2Val)
                && dataSource1[i].xianbingshi.toUpperCase().includes(bz3Val)){
          data.push(dataSource1[i]);
        }
        }
        else {
          if (dataSource1[i].biaohao.toUpperCase().includes(bhVal)
                  && dataSource1[i].jiuzhenyisheng.toUpperCase().includes(ysVal)
                  && dataSource1[i].shuming.toUpperCase().includes(smVal)
                  && dataSource1[i].zuozhe.toUpperCase().includes(zzVal)
                  && dataSource1[i].xingming.toUpperCase().includes(hzVal)
                  && dataSource1[i].shenhe.toUpperCase().includes(shenheVal)
                  && dataSource1[i].xianbingshi.toUpperCase().includes(bz1Val)
                  && dataSource1[i].xianbingshi.toUpperCase().includes(bz2Val)
                  && dataSource1[i].xianbingshi.toUpperCase().includes(bz3Val)
                  &&( (((dataSource1[i].year*366+dataSource1[i].month*31+dataSource1[i].day*1)>=DateNumber1)
                  && ((dataSource1[i].year*366+dataSource1[i].month*31+dataSource1[i].day*1)<=DateNumber2))
          || (((dataSource1[i].month*31+dataSource1[i].day*1)>=DateNumber1)
                  && ((dataSource1[i].month*31)<=DateNumber2)&&year2Val==""&&year1Val=="")))
            data.push(dataSource1[i]);
        }
      }
    }
    renderData();
  }
  function enterKeyPress(event) {
    // 检查按下的按键是否为回车键，回车键的keyCode是13
    if (event.keyCode === 13) {
      // search();
      startselect();
    }
  }
  function create(){
    window.location.href="/wxya/create";
  }
  function update(userid){
    window.open ("/wxya/edit/" + userid);
  }
  function deleteD(userid){
    var result=confirm("确认删除当前内容吗？");
    if(result){
      // 发送 DELETE 请求
      fetch('/wxya/'+userid, {
        method: 'DELETE',
        headers: {
          'Content-Type': 'application/json' // 可根据实际情况设置请求头
        },
      })
              .then(response => {
                if (response.ok) {
                  alert("删除成功！");
                  start();
                  renderData();
                } else {
                  alert("发生错误，删除失败！");
                }
              })
              .catch(error => {
                // 捕获网络错误等异常
                alert("网络错误，删除失败！");
              });

    }
  }
  function pageSearch(event) {
    // 检查按下的按键是否为回车键，回车键的keyCode是13
    if (event.keyCode === 13) {
      pageOn();
    }
  }
  function pageOn(){
    var afterPage=pageChage.value;
    if(afterPage<1||afterPage>pageSum){
      alert("输入的页码错误！");
      return;
    }
    currentPage=afterPage;
    renderData();
  }
  function table2Excel(filename) {
    $("#tbl_list").table2excel({
      exclude: ".noExl",//class="noExl"的列不导出
      name: "Excel Document Name",
      filename: filename  + ".xlsx",//文件名称
      fileext: ".xlsx",//文件后缀名
      exclude_img: true,//导出图片
      exclude_links: true,//导出超链接
      exclude_inputs: true//导出输入框内容
    });
  }
</script>
<script src="/js/jquery.table2excel.js"></script>